/*
 * Created on Feb 23, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI.tree;

import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.border.EtchedBorder;
import javax.swing.text.*; 
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.beans.*;

import littlecyberwar.ui.SpringUtilities;

import ModuleBuilder.*;
import ModuleBuilder.UI.tree.UnitProfileEditor.AttributePanel;
import ModuleBuilder.model.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ScenarioDetailEditor extends JPanel 
	implements ActionListener, iEditorPanel {
	
	private static final String CLEAR_CMD="Clear".intern();	// clear all the fields w/o saving
	private static final String SAVE_CMD="Save".intern();	// save and create a new node (if does not exist previously) with current data

	// hash key used in the ScenarioDetailModel hashMap
	public static final String _scenarioDetail_name = "n".intern();
	public static final String _scenarioDetail_scale = "s".intern();
	public static final String _scenarioDetail_distanceUnit = "d".intern();
	public static final String _scenarioDetail_notes = "t".intern();
	
	public static final Double _defaultMapScale = new Double(40);   // 40 pixels = 1 inch
	public static final String _defaultMapDistanceUnit = "inch";

	
//	private JLabel nameLabel;
//	private JTextField nameTextField;
//	private JLabel mapScaleLabel;
//	private JTextField mapScaleTextField;
//	private JLabel distanceUnitLabel;
//	private JTextField distanceUnitTextField;
//	
//	private JLabel notesLabel;
//	private JTextArea notesArea;
	
	private ScenarioDetailModel model;	// the Current model being operated at.
	
	ScenarioDetailPanel detailPanel;
	JButton clearButton;
	JButton saveButton;
	
	private GameTreeEditor gameTreeEditor;	
	JFrame frame;
	
	public ScenarioDetailEditor(GameTreeEditor in) {
		gameTreeEditor = in;
		
//		nameLabel = new JLabel("Scenario Name:");
//		mapScaleLabel = new JLabel("Map Scale");
//		distanceUnitLabel = new JLabel("Distance Unit");
//		notesLabel = new JLabel("Scenario Notes");
//		
//		nameTextField = new JTextField();
//		mapScaleTextField = new JTextField();
//		distanceUnitTextField = new JTextField();
//		notesArea = new JTextArea();
//		
//		// now do the layout
//		this.setLayout(new GridLayout(0,2));
//		JPanel sdPanel = new JPanel();
//		sdPanel.setLayout(new BoxLayout(sdPanel, BoxLayout.PAGE_AXIS));
//		sdPanel.add(nameLabel);
//		sdPanel.add(nameTextField);
//		nameLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		nameTextField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//
//		JPanel msPanel = new JPanel();
//		msPanel.setLayout(new BoxLayout(msPanel, BoxLayout.PAGE_AXIS));
//		msPanel.add(mapScaleLabel);
//		msPanel.add(mapScaleTextField);
//		mapScaleLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		mapScaleTextField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//
//		
//		JPanel duPanel = new JPanel();
//		duPanel.setLayout(new BoxLayout(duPanel, BoxLayout.PAGE_AXIS));
//		duPanel.add(distanceUnitLabel);
//		duPanel.add(distanceUnitTextField);
//		distanceUnitLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		distanceUnitTextField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//
//		
//		JPanel notePanel = new JPanel();
//		notePanel.setLayout(new BoxLayout(notePanel, BoxLayout.PAGE_AXIS));
//		notePanel.add(notesLabel);
//		JScrollPane noteScrollPane = 
//		    new JScrollPane(notesArea,
//		                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
//		                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//		notesArea.setEditable(true);
//		notePanel.add(noteScrollPane);
//		notesLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		noteScrollPane.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//
//		
//
//		// now need to set some size limitations to the components
//		Dimension d = nameTextField.getPreferredSize();
//		d.width = 200;
//		nameTextField.setMinimumSize(d);
//		nameTextField.setPreferredSize(d);
//		nameTextField.setMaximumSize(d);
//
//		d = mapScaleTextField.getPreferredSize();
//		d.width = 200;
//		mapScaleTextField.setMinimumSize(d);
//		mapScaleTextField.setPreferredSize(d);
//		mapScaleTextField.setMaximumSize(d);
//
//		d = distanceUnitTextField.getPreferredSize();
//		d.width = 200;
//		distanceUnitTextField.setMinimumSize(d);
//		distanceUnitTextField.setPreferredSize(d);
//		distanceUnitTextField.setMaximumSize(d);
		
		detailPanel = new ScenarioDetailPanel();
		
		// Create the Button panel in the bottom
		clearButton = new JButton(CLEAR_CMD);
		clearButton.setActionCommand(CLEAR_CMD);
		clearButton.addActionListener(this);
 
		saveButton = new JButton(SAVE_CMD);
		saveButton.setActionCommand(SAVE_CMD);
		saveButton.addActionListener(this);
 
 		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.add(clearButton);
		buttonPanel.add(saveButton);

		// overall layout
		this.setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));
//		sdPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		msPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		duPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		notePanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
//		
//		this.add(sdPanel);
//		this.add(msPanel);
//		this.add(duPanel);
//		this.add(notePanel);
		
		
		this.add(detailPanel);
		buttonPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		this.add(buttonPanel);
	
		Color bc = this.getBackground();
		this.setBorder(BorderFactory.createMatteBorder(10,10,10,10, bc));
		
		frame = ModuleBuilderMain.getInstance();
	}
	
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
    
		if (CLEAR_CMD.equals(command)) {
			handleClearCmd();
			
		} else if (SAVE_CMD.equals(command)) {
			handleSaveCmd();
			
		}
		return;
	}

	protected void handleClearCmd() {
		// delete all the attributes from the UI
		//
		detailPanel.setNameTextField(null);
		detailPanel.setMapScaleTextField(null);
		detailPanel.setDistanceUnitTextField(null);
	}
	
	/**
	 * If there already is a model, then we'll just write over what's in it.
	 * If there isn't currently a model, then we'll create a new one.
	 *
	 */
	protected void handleSaveCmd(){
		// boolean isNew = false;
		if ( model == null ) {
			model = new ScenarioDetailModel(null, GameTreeEditor.SCENARIO_DETAIL_EDITOR, false, this);
			// isNew = true;
		} else {
			// remove all the old attributes 
			model.clear();
		}
		boolean status = loadModelFromUI( model );
		
		if ( status ) {
			DefaultMutableTreeNode tn = null;
			// reload in case the name have changed
			tn = gameTreeEditor.getCurrentNode();
			gameTreeEditor.reloadTreeModel();

			// the notification is always done wether a brand new node or not, since 
			// new attributes might have been added.
			model = null;
		}
		return;
	}

	/**
	 * 
	 * @param model
	 * @return true if everything is ok, false if we should abort
	 */
	private boolean loadModelFromUI( ScenarioDetailModel model ) {
		boolean val = true;
		String name = detailPanel.getNameTextField().getText();
		String scale = detailPanel.getMapScaleTextField().getText();
		String distance = detailPanel.getDistanceUnitTextField().getText();
		Document doc = detailPanel.getNotesArea().getDocument();
		
		/**
		 * Validate the fields, the scale field must be a number
		 */
		Double scaleDouble=null;
		try {
			scaleDouble = new Double(scale);
		} catch (NumberFormatException ex){
			JOptionPane.showMessageDialog(frame,
				    "The Scale field must be a number.",
				    "Input Error",
				    JOptionPane.ERROR_MESSAGE);
			val = false;
		}
		model.put(_scenarioDetail_name, name);		
		model.put(_scenarioDetail_scale, scaleDouble);
		model.put(_scenarioDetail_distanceUnit, distance);
		model.put(_scenarioDetail_notes, doc);
		return ( val );
	}
	
	public void setModel ( GameModel nodeIn ){
		model = (ScenarioDetailModel)nodeIn;
		handleClearCmd();
		if ( model != null ) {
			String name = (String)model.get(_scenarioDetail_name);
			Double scale = (Double)model.get(_scenarioDetail_scale);
			String distance = (String)model.get(_scenarioDetail_distanceUnit);
			Document doc = (Document)model.get(_scenarioDetail_notes);
			String scaleStr=null;
			if ( scale != null ){
				scaleStr = scale.toString();
			}
			if ( name != null ){
				detailPanel.setNameTextField(name);
				detailPanel.setMapScaleTextField(scaleStr);
				detailPanel.setDistanceUnitTextField(distance);
			}
			if ( doc != null ){
				detailPanel.setNotesArea(doc);
			}
		}
		return;
	}

}
